package com.dishdex.menuhub.console.controller;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.CannedAccessControlList;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;


/**
 * ClassName:
 * Package: com.dishdex.menuhub.console.controller
 * Description:
 *
 * @Author:
 * @Create 2025/10/14 10:41
 * @Version 1.0
 */
@RestController
public class TestController {

    @RequestMapping("file/save")
    public String fileSave(@RequestParam("file") MultipartFile file) throws IOException {
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        String region = "cn-hangzhou";
        String accessKeyId = System.getenv("OSS_ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("OSS_ACCESS_KEY_SECRET");
        DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
        ClientBuilderConfiguration clientConfig = new ClientBuilderConfiguration();
        clientConfig.setSignatureVersion(SignVersion.V4);

        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .region(region)
                .credentialsProvider(credentialProvider)
                .clientConfiguration(clientConfig)
                .build();

        String bucketName = "demo1fnoiegno";

        ossClient.createBucket(bucketName);
        ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMM/dd");
        String date = now.format(formatter);

        String uniqueString = UUID.randomUUID().toString();

        String originalFileName = file.getOriginalFilename();
        int lastDotIndex = originalFileName.lastIndexOf(".");
        String fileType = originalFileName.substring(lastDotIndex + 1);

        String fileName;
        if (fileType.equals("jpg") || fileType.equals("png") || fileType.equals("gif")) {
            InputStream inputStream = file.getInputStream();
            BufferedImage image = ImageIO.read(inputStream);
            Integer width = image.getWidth();
            Integer height = image.getHeight();
            fileName = "image/" + date + "/" + uniqueString + "_" + width + "x" + height + "." + fileType;
        } else {
            fileName = "file/" + date + "/" + uniqueString + "." + fileType;
        }

        ossClient.putObject(bucketName, fileName, file.getInputStream());
        ossClient.shutdown();

        String url = "https://" + bucketName + "." + "oss-cn-hangzhou.aliyuncs.com/" + fileName;
        return url;
    }
}
